package leetCode.firstWeek;

public class Code_5 {
  public String longestPalindrome(String s) {
      int contious = 1;
    if (s.length() == 0) {
      return "";
    }
    for(int tmp = 0;tmp<s.length()-1;tmp++){
        if(s.charAt(tmp) != s.charAt(tmp+1)){
            contious = 0;
        }
    }
    if(contious == 1){
        return s;
    }
    int max = 0, finalStart = 0, finalEnd = 0;
    for (int i = 0; i < s.length(); i++) {
      for (int j = i; j < s.length(); j++) {
        if (isPalindrome(s, i, j)) {
          if (j - i > max) {
            finalStart = i;
            finalEnd = j;
            max = j - i;
          }
        }
      }
    }
    return s.substring(finalStart, finalEnd + 1);
  }

  public boolean isPalindrome(String s, int startIndex, int endIndex) {
    for (int i = startIndex; i <= (endIndex + startIndex) / 2; i++) {
      if (s.charAt(i) != s.charAt(startIndex + endIndex - i)) {
        return false;
      }
    }
    return true;
  }
}
